#include "qmemerydataproperty.h"

#include "../xmlobject.h"
#include "../qcommonstruct.h"
#include "../qpropertyfactory.h"

QMemeryDataProperty::QMemeryDataProperty(QAbstractProperty *parent):
    QAbstractProperty(parent)
{
    set_property("type","Memery");
}

void QMemeryDataProperty::toObject(XMLObject *xml)
{
    if(xml!=NULL)
    {
        xml->clear();

        xml->set_title(m_propertys.value("name").toString());

        QMap<QString,QVariant> pros=m_propertys;
        pros.remove("name");
        pros.remove("value");
        QMapIterator<QString,QVariant>      it(pros);

        while(it.hasNext())
        {
            it.next();
            xml->set_property(it.key(),it.value().toString());
        }

        MemeryValueItems items=get_value().value<MemeryValueItems>();

        XMLObject *obj;
        foreach(tagMemeryValueInfo info,items)
        {
            obj=new XMLObject(xml);
            obj->set_title("Item");
            obj->set_property("property",info.m_propertyName);
            obj->set_property("id",info.m_uuid);
            obj->set_property("property_to_value_script",info.m_property_to_value_script);
            obj->set_property("value_to_property_script",info.m_value_to_property_script);
        }
    }
}

void QMemeryDataProperty::fromObject(XMLObject *xml)
{
    if(xml!=NULL)
    {

        QMapIterator<QString,QString>      it(xml->get_propertys());

        while(it.hasNext())
        {
            it.next();
            m_propertys.insert(it.key(),it.value());
        }
        QList<XMLObject*>   children=xml->get_children();
        MemeryValueItems items;
        foreach(XMLObject* child,children)
        {
            tagMemeryValueInfo info;
            info.m_propertyName=child->get_property("property");
            info.m_uuid=child->get_property("id");
            info.m_property_to_value_script=child->get_property("property_to_value_script");
            info.m_value_to_property_script=child->get_property("value_to_property_script");
            items.append(info);
        }
        m_value.setValue<MemeryValueItems>(items);
        refresh();
    }
}

QString QMemeryDataProperty::get_value_text()
{
    MemeryValueItems c=get_value().value<MemeryValueItems>();
    if(c.size()==0)
    {
        return "None";
    }
    else
    {
        return QString("count:%1").arg(c.size());
    }
}
QIcon QMemeryDataProperty::get_value_icon()
{
    return QIcon();
}

QString QMemeryDataProperty::get_property_name()
{
    return "Memery";
}

bool QMemeryDataProperty::cmp(const QVariant &value)
{
    MemeryValueItems items1=get_value().value<MemeryValueItems>();
    MemeryValueItems items2=value.value<MemeryValueItems>();
    if(items1.size()!=items2.size())
    {
        return false;
    }

    tagMemeryValueInfo info1,info2;

    for(int i=0;i<items1.size();i++)
    {
        info1=items1.at(i);
        info2=items2.at(i);
        if(!(info1==info2))
        {
            return false;
        }
    }

    return true;
}

qRegisterProperty(QMemeryDataProperty)
